package com.youtui.manager.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youtui.common.constant.ResultMessage;
import com.youtui.common.util.DateUtils;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.entity.PredictIncomeEntity;
import com.youtui.manager.service.IPredictIncomeService;

@Controller
@RequestMapping("/predict")
public class PredictIncomeController extends BaseController {

	@Autowired
	IPredictIncomeService predictIncomeService;

	@RequestMapping("/list_7.do")
	@ResponseBody
	public ResultMessage findPageList(Long devId, Date startDate, Date endDate) {

		if (devId == null || startDate == null || endDate == null) {
			return ResultUtil.result(ResultUtil.code_success, "请求参数错误");
		}

		int count = 7;
		int days = DateUtils.diffDay(startDate, endDate);
		
		if(count<days){
			count=days;
		}
		List<PredictIncomeEntity> list = predictIncomeService.findPredictIncomeEntityList_7(devId, startDate, endDate);
		Map<Date , PredictIncomeEntity> map = new HashMap<Date, PredictIncomeEntity>();
		for(PredictIncomeEntity entity:list){
			map.put(entity.getCurdate(), entity);
		}
		
		List<PredictIncomeEntity> listtmp = new ArrayList<PredictIncomeEntity>();

		for(int i=0;i<count;i++){
			Date dateTmp=DateUtils.addDays(startDate, i);
			PredictIncomeEntity piTmp=map.get(dateTmp);
			if(piTmp == null){
				piTmp = new PredictIncomeEntity();
				piTmp.setCurdate(dateTmp);
				piTmp.setUv(0);
				piTmp.setPredict(new BigDecimal(0));
			} 
			listtmp.add(piTmp);
				
		}
		
		

		return ResultUtil.result(ResultUtil.code_success, "开发者预收入列表", listtmp);
	}

	@RequestMapping("/loadData.do")
	@ResponseBody
	public ResultMessage loadData(Long devId) {

		return ResultUtil.result(ResultUtil.code_success, "预收入信息查询");

	}
	
	@RequestMapping("/export.do")
	@ResponseBody
	public void export(Long devId, Date startDate, Date endDate) throws Exception {
		if (devId == null || startDate == null || endDate == null) {
			return ;
		}
 
		List<PredictIncomeEntity> reportList = predictIncomeService.findPredictIncomeEntityList_7(devId, startDate, endDate);
		String fileName = "预计收入报表" + DateUtils.getTimestamp();
		Map<String, Object> dataMap = new HashMap<String, Object>();
 
		excelUtil.excelData(request, response, fileName, "predict_excel", reportList, dataMap);
	}

}
